package game.utils;

import hx.utils.ArraySort;

class XmlTools {
	/**
	 * 删除去重的xml节点
	 */
	public static function removeDuplicateNodes(xml:Xml):Xml {
		var newXml = Xml.createDocument();
		var root = Xml.createElement("TextureAtlas");
		newXml.insertChild(root, 0);
		if (xml.nodeType == Xml.Document) {
			xml = xml.firstElement();
		}
		for (key in xml.attributes()) {
			root.set(key, xml.get(key));
		};
		var list = [];
		var keys:Map<String, Bool> = [];
		for (item in xml.elements()) {
			if (!keys.exists(item.get("name"))) {
				keys.set(item.get("name"), true);
				list.push(item);
			}
		}
		list.sort((a, b) -> {
			var a_name = a.get("name");
			var b_name = b.get("name");
			return ArraySort.sort(a_name, b_name);
		});
		for (item in list) {
			root.addChild(item);
		}
		return newXml;
	}
}
